package com.netty.modbus.trigger;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * description: ServerIdleStateTrigger <br>
 * date: 2021/9/30 16:03 <br>
 * @author: lwy <br>
 * version: 1.0 <br>
 */
public class ServerIdleStateTrigger extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt){
        if (evt instanceof IdleStateEvent){
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.READER_IDLE){
                // 在规定时间内没有收到客户端的上行数据, 主动断开连接
                String url = ctx.channel().remoteAddress().toString();
                System.out.println("没有数据发送了，来自" + url + "断开连接了");
                ctx.disconnect();
            }
        }
    }


}
